package pl.lodz.p.services;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import pl.lodz.p.beans.utils.HibernateUtil;
import pl.lodz.p.model.Doctor;
import pl.lodz.p.model.Patient;
import pl.lodz.p.model.enums.Gender;
import pl.lodz.p.model.enums.Specialization;

public class PatientServiceImpl implements PatientService {

	public String save(Patient d) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(d);
			tx.commit();
			return "saved";
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
				e.printStackTrace();
				return "error";
			}
		} finally {
			session.close();
		}
		return "error";
	}

	public List<Patient> findAll() {
		List<Patient> res = new ArrayList<Patient>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			res = session.createQuery("from Patient").list();
			return res;
		} catch (Exception e) {
				e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	public List<Patient> findPatientsByFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Patient> findBySpecialization(Specialization spec) {
		// TODO Auto-generated method stub
		return null;
	}

	public Patient findByPesel(String pesel) {
		Patient res;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			res = (Patient) session.createCriteria(Patient.class)
					.add(Restrictions.like("pesel", pesel))
					.uniqueResult();
			return res;
		} catch (Exception e) {
				e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	public List<Patient> findByLastName(String lastName) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Patient> findByMany(String firstName, String lastName,
			String pesel, Gender gender) {
		List<Patient> res = new ArrayList<Patient>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Criteria criteria = session.createCriteria(Patient.class);
			if (firstName != null && !firstName.isEmpty()){
				criteria.add(Restrictions.like("firstName", firstName));
			}if (lastName != null && !lastName.isEmpty()){
				criteria.add(Restrictions.like("lastName", lastName));
			}if (pesel != null && !pesel.isEmpty()){
				criteria.add(Restrictions.like("pesel", pesel));
			}if (gender != null){
				criteria.add(Restrictions.eq("gender", gender));
			}
			res = criteria.list();
			return res;
		} catch (Exception e) {
				e.printStackTrace();
		} finally {
			session.close();
		}
		return null;
	}

	public String saveOrUpdate(Patient p) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(p);
			tx.commit();
			return "saved";
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
				e.printStackTrace();
				return "error";
			}
		} finally {
			session.close();
		}
		return "error";
	}
}
